<?php defined('SYSPATH') or die('No direct script access.');

/**
 * 提现审核
 * Class Controller_Admin_Finance_Supplier
 */
class Controller_Admin_Finance_Supplier_Withdraw extends Stourweb_Controller
{

    function before()
    {
        parent::before();
        $this->assign('prefix_url','finance/admin/finance/supplier');
    }

    //提现审核
    public function action_aduit()
    {
        $action = $this->params['action'];
        if(!$action)
        {
            $this->display('admin/finance/supplier/withdraw/aduit');

        }
        switch ($action)
        {
            case 'read':
                $this->withdraw_list();
                break;
            case 'save':
                $id = $_POST['id'];
                $status = $_POST['status'];
                $audit_description = $_POST['audit_description'];
                $certificate = $_POST['certificate'];
                if($status)
                {
                    $data = array(
                        'status'=>$status,
                        'audit_description'=>$audit_description,
                        'certificate'=>$certificate,
                        'finishtime'=>time(),
                    );
                    DB::update('supplier_finance_drawcash')->set($data)
                        ->where('id','=',$id)->and_where('status','=',0)
                        ->execute();
                    //审核完成,收支明细记录添加
                    if($status == 1)
                    {
                        $row = DB::select()->from('supplier_finance_drawcash')->where('id','=',$id)->execute()->current();
                        $supplier = DB::select()->from('supplier')->where('id','=',$row['supplierid'])->execute()->current();
                        $amount = $row['withdrawamount'];
                        $remark = '供应商'.$supplier['suppliername'].'提现成功!';
                        Model_Finance_Record::add_finance_record($amount,2,'供应商提现',$remark);
                    }

                }
                echo  json_encode(array('status'=>1));
                break;
        }





    }


    //审核列表
    private function withdraw_list()
    {
        $start = $_GET['start'];
        $limit = $_GET['limit'];
        $status = $_GET['status'];
        $keyword = $_GET['keyword'];
        $startdate = $_GET['startdate'];
        $enddate = $_GET['enddate'];
        $where = " a.id>0 and b.id>0";
        switch ($status)
        {
            case 1://未审核
                $where .= " and a.status=0";
                break;
            case 2://审核未通过
                $where .=" and a.status=2";
                break;
            case 3:
                $where .=" and a.status=1";
                break;
        }
        if($keyword)
        {
            $where .=" and b.suppliername like '%$keyword%'";
        }
        if($startdate)
        {
            $where .=" and a.addtime >".strtotime($startdate);
        }
        if($enddate)
        {
            $where .=" and a.addtime <=".strtotime($enddate);
        }

        $total_sql="select COUNT(*) as num from sline_supplier_finance_drawcash as a LEFT JOIN sline_supplier as b on a.supplierid=b.id WHERE $where ";
        $total = DB::query(1,$total_sql)->execute()->get('num');

        $sql="select a.*,b.suppliername from sline_supplier_finance_drawcash as a LEFT JOIN sline_supplier as b on a.supplierid=b.id WHERE $where ORDER by a.id desc limit $start,$limit";
        $list = DB::query(1,$sql)->execute()->as_array();
        foreach ($list as &$l)
        {
            $l['addtime'] = date('Y-m-d H:i:s',$l['addtime']);
            $l['finishtime'] = $l['finishtime'] ? date('Y-m-d H:i:s',$l['finishtime']) : '';
            $l['proceeds_type_title'] = Model_Finance_Supplier::get_proceeds_type($l['proceeds_type']);
            $l = array_merge($l,Model_Finance_Supplier::get_supplier_price_info($l['supplierid']));
        }
        unset($l);


        echo json_encode(array('lists'=>$list,'total'=>$total));
    }


    //审核修改
    public function action_modify()
    {
        $id = $this->params['id'];
        $sql = "select a.*,b.suppliername,b.linkman,b.mobile  from sline_supplier_finance_drawcash as a LEFT JOIN sline_supplier as b on a.supplierid=b.id WHERE a.id=$id";
        $info = DB::query(1,$sql)->execute()->current();
        if($info)
        {
            $info = array_merge($info,Model_Finance_Supplier::get_supplier_price_info($info['supplierid']));
            $info['proceeds_type_title'] = Model_Finance_Supplier::get_proceeds_type($info['proceeds_type']);
        }
        $this->assign('info',$info);
        $this->display('admin/finance/supplier/withdraw/modify_withdraw');

    }

    /*
     * 生成excel页面
     * */
    public function action_excel()
    {

        $this->assign('statusnames', Model_Member_Order::$order_status);
        $this->display('admin/finance/supplier/withdraw/excel');
    }

    public function action_genexcel()
    {
        $timetype = $this->params['timetype'];
        $starttime = empty($_GET['starttime'])? null:strtotime($_GET['starttime'].' 00:00:00');
        $endtime =empty($_GET['endtime'])?null: strtotime($_GET['endtime'].' 23:59:59');
        $time_arr = array();

        switch ($timetype)
        {
            case 1:
                $time_arr = St_Functions::back_get_time_range(1);
                break;
            case 2:
                $time_arr = St_Functions::back_get_time_range(2);
                break;
            case 3:
                $time_arr = St_Functions::back_get_time_range(3);
                break;
            case 5:
                $time_arr = St_Functions::back_get_time_range(5);
                break;
            case 6:
                $time_arr = array($starttime, $endtime);
                break;
        }

        $where  = " a.addtime > $time_arr[0] and  a.addtime < $time_arr[1] " ;
        $sql="select a.*,b.suppliername from sline_supplier_finance_drawcash as a LEFT JOIN sline_supplier as b on a.supplierid=b.id WHERE $where ORDER by a.id desc ";
        $info = DB::query(1,$sql)->execute()->as_array();
        $this->gene_excel($info);


    }

    protected function gene_excel($order_list)
    {


        $titles = array('申请时间','供应商名称','提现金额','提现方式','提现状态','完成时间');
        $rows = array();
        foreach ($order_list as $row)
        {

            $row['finishtime'] = $row['finishtime'] ? date('Y-m-d H:i:s',$row['finishtime']) : '--';
            $row['proceeds_type_title'] = Model_Finance_Supplier::get_proceeds_type($row['proceeds_type']);
            //提现状态
            $withdraw_status = '待审核';
            if($row['status'] == 1)
            {
                $withdraw_status = '已提现';
            }
            else if($row['status'] == 2)
            {
                $withdraw_status = '未通过';
            }
            $rows[] = array(
                date('Y-m-d H:i:s',$row['addtime']),
                $row['suppliername'],
                $row['withdrawamount'],
                $row['proceeds_type_title'],
                $withdraw_status,
                $row['finishtime']
            );
        }
        $td_width = array(25,25,10,15,15,20);
        $col_style = array('string','string','string','string','string','string');
        St_File::export_excel($rows, $titles, $td_width, $col_style);

    }

}